/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.plugins  
 */
package com.apache.bid.service.plugins;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.entity.BidApply;
import com.apache.bid.manager.BidApplyManager;
import com.apache.bid.manager.BidItemGroupManager;
import com.apache.bid.manager.BidPubManager;
import com.apache.bid.manager.BidSessionManager;
import com.apache.cache.util.Validator;

/**
 * description:  统一下保存竞买人插件
 * @author Hou Dayu 创建时间：2016-3-23  
 * 
 */
public class UnitySaveApplyPluginImpl implements PluginConnector {

	protected Logger log = Logger.getLogger(getClass());
	protected BidSessionManager bidSessionManager;
	protected BidPubManager bidPubManager;
	protected BidItemGroupManager bidItemGroupManager;
	private BidApplyManager bidApplyManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo param) throws Exception {
		log.info("开始保存竞买人，并且加载缓存...");
		ResultEntity entity = new ResultEntity();
		String applyId = saveApply(param);
		if (Validator.isNotNull(applyId)) {
			entity.setResult("true");
			entity.setMessage("保存成功");
			entity.setEntity(applyId);
		} else {
			entity.setResult("false");
			entity.setMessage("保存失败");
		}
		return entity;
	}

	private String saveApply(ParamsVo param) {
		//存库
		String applyId = bidApplyManager.saveInfo(param);
		if (Validator.isNotNull(applyId)) {

			//			ParamsVo<BidApply> applyVo = new ParamsVo<BidApply>();
			//			applyVo.setInfoId(applyId);
			BidApply apply = (BidApply) param.getObj();//(BidApply)bidApplyManager.getInfoById(applyVo);

			//加载缓存
			BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("apply");
			boolean mark = cache.createCacheObject(apply);
			if (!mark) {
				log.info("加载缓存失败");
			}
		}
		return applyId;
	}

	public void setBidSessionManager(BidSessionManager bidSessionManager) {
		this.bidSessionManager = bidSessionManager;
	}

	public void setBidPubManager(BidPubManager bidPubManager) {
		this.bidPubManager = bidPubManager;
	}

	public void setBidItemGroupManager(BidItemGroupManager bidItemGroupManager) {
		this.bidItemGroupManager = bidItemGroupManager;
	}

	public void setBidApplyManager(BidApplyManager bidApplyManager) {
		this.bidApplyManager = bidApplyManager;
	}

}
